package com.zyh.scs.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyh.scs.admin.api.constant.RechargeTypeEnum;
import com.zyh.scs.admin.api.dto.UserPaymentDTO;
import com.zyh.scs.admin.api.entity.UserPayment;
import com.zyh.scs.admin.api.utils.UserTypeToNameUtil;
import com.zyh.scs.admin.api.vo.PaymentExcelVO;
import com.zyh.scs.admin.mapper.UserPaymentMapper;
import com.zyh.scs.admin.service.UserPaymentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author hwh
 * @ClassName: 支付管理api接口实现类$
 * @Description: $
 * @Date $
 * @Version 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserPaymentServiceImpl extends ServiceImpl<UserPaymentMapper, UserPayment> implements UserPaymentService {

    private final UserPaymentMapper userPaymentMapper;

    @Override
    public IPage getPaymentPage(Page page, UserPaymentDTO userPaymentDTO) {
        Page<UserPayment> userPaymentPage = new Page<>(page.getCurrent(), page.getSize());
        LambdaQueryWrapper<UserPayment> queryWrapper = getPaymentLambdaQueryWrapper(userPaymentDTO, 2);
        return this.page(userPaymentPage, queryWrapper);
    }

    private static @NotNull LambdaQueryWrapper<UserPayment> getPaymentLambdaQueryWrapper(UserPaymentDTO userPaymentDTO, Integer isExcel) {
        if (StrUtil.isBlank(userPaymentDTO.getBeginTime()) && StrUtil.isBlank(userPaymentDTO.getEndTime())
                && userPaymentDTO.getUserId() == null && StrUtil.isBlank(userPaymentDTO.getUserJobNumber())
                && StrUtil.isBlank(userPaymentDTO.getPaymentChannel()) && StrUtil.isBlank(userPaymentDTO.getAccountType())
                && StrUtil.isBlank(userPaymentDTO.getRechargeStream()) && StrUtil.isBlank(userPaymentDTO.getThirdRechargeStream())
                && StrUtil.isBlank(userPaymentDTO.getPhone()) && StrUtil.isBlank(userPaymentDTO.getOrderId()) && StrUtil.isBlank(userPaymentDTO.getGuid())
                && StrUtil.isBlank(userPaymentDTO.getOutOrderNo())) {
            userPaymentDTO.setBeginTime(LocalDate.now().plusDays(-3).toString());
        }
        LambdaQueryWrapper<UserPayment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(userPaymentDTO.getRechargeStream()), UserPayment::getRechargeStream, userPaymentDTO.getRechargeStream())
                .eq(StrUtil.isNotBlank(userPaymentDTO.getThirdRechargeStream()), UserPayment::getThirdRechargeStream, userPaymentDTO.getThirdRechargeStream())
                .eq(userPaymentDTO.getUserId() != null, UserPayment::getUserId, userPaymentDTO.getUserId())
                .eq(StrUtil.isNotBlank(userPaymentDTO.getUserJobNumber()), UserPayment::getUserJobNumber, userPaymentDTO.getUserJobNumber())
                .eq(StrUtil.isNotBlank(userPaymentDTO.getOutOrderNo()), UserPayment::getOutOrderNo, userPaymentDTO.getOutOrderNo())
                .eq(StrUtil.isNotBlank(userPaymentDTO.getOrderId()), UserPayment::getOrderId, userPaymentDTO.getOrderId())
                .eq(StrUtil.isNotBlank(userPaymentDTO.getPhone()), UserPayment::getPhone, userPaymentDTO.getPhone())
                .eq(StrUtil.isNotBlank(userPaymentDTO.getPaymentChannel()), UserPayment::getPaymentChannel, userPaymentDTO.getPaymentChannel())
                .eq(StrUtil.isNotBlank(userPaymentDTO.getGuid()), UserPayment::getGuid, userPaymentDTO.getGuid())
                .eq(StrUtil.isNotBlank(userPaymentDTO.getAccountType()), UserPayment::getAccountType, userPaymentDTO.getAccountType())
                .ge(StrUtil.isNotBlank(userPaymentDTO.getBeginTime()), UserPayment::getTransactionTime, UserTypeToNameUtil.stringToLocalBeginTime(userPaymentDTO.getBeginTime()))
                .le(StrUtil.isNotBlank(userPaymentDTO.getEndTime()), UserPayment::getTransactionTime, UserTypeToNameUtil.stringToLocalEndTime(userPaymentDTO.getEndTime()))
                .eq(UserPayment::getDelFlag, 0)
                .orderByDesc(UserPayment::getTransactionTime, UserPayment::getPaymentId);

        if (isExcel == 1) {
            queryWrapper.last(" limit 100000");
        }
        return queryWrapper;
    }

    @Override
    public List<PaymentExcelVO> getPaymentExcel(UserPaymentDTO userPaymentDTO) {
        LambdaQueryWrapper<UserPayment> queryWrapper = getPaymentLambdaQueryWrapper(userPaymentDTO, 1);
        List<UserPayment> userPaymentList = userPaymentMapper.selectList(queryWrapper);
        List<PaymentExcelVO> paymentExcelVOList = new ArrayList<>();
        AtomicInteger index = new AtomicInteger(1);
        userPaymentList.forEach(a -> {
            PaymentExcelVO paymentExcelVO = new PaymentExcelVO();
            BeanUtil.copyProperties(a, paymentExcelVO);
            paymentExcelVO.setPayNumber(a.getPayNumber());
            paymentExcelVO.setIndex(index.getAndIncrement());
            if (a.getUserType() != null) {
                paymentExcelVO.setUserTypeName(UserTypeToNameUtil.getUserType(a.getUserType()));
                switch (a.getAccountType()) {
                    case "1":
                        paymentExcelVO.setAccountType(RechargeTypeEnum.MEAL_ACCOUNT.getDesc());
                        break;
                    case "2":
                        paymentExcelVO.setAccountType(RechargeTypeEnum.CASH_ACCOUNT.getDesc());
                        break;
                }
                switch (a.getPaymentChannel()) {
                    case "1":
                        paymentExcelVO.setPaymentChannel("满客宝");
                        break;
                    case "2":
                        paymentExcelVO.setPaymentChannel("京东锦礼");
                        break;
                    case "3":
                        paymentExcelVO.setPaymentChannel("后台");
                        break;
                }
            }
            paymentExcelVOList.add(paymentExcelVO);
        });

        return paymentExcelVOList;
    }
}
